"""Tests the ajax handler"""

import socket
import threading
import unittest
import urllib2
import mox
import yaml
import tornado.httpclient
import tornado.httpserver
import tornado.ioloop
from gaweb.master import excepts
from gaweb.master import model
from gaweb.master.handlers import ajax_handler



class AjaxHandlerTest(unittest.TestCase):
    """Tests for the AjaxHandler class."""

    def setUp(self):
        self.mox = mox.Mox()
        self.master = self.mox.CreateMock(model.Master)
        self.app = tornado.web.Application([(r'/ajax', ajax_handler.AjaxHandler, dict(master=self.master))])
        self.ioloop = tornado.ioloop.IOLoop.instance()
        self.server = tornado.httpserver.HTTPServer(self.app, io_loop=self.ioloop)

        self.server.listen(9999)
        
        self.t = threading.Thread(target=self.ioloop.start)
        self.t.start()

    def tearDown(self):
        self.ioloop.stop()
        self.server.stop()

    def test_init(self):
        self.master.make_session('127.0.0.1').AndReturn('abcdef')
        self.mox.ReplayAll()

        resp = yaml.load(urllib2.urlopen('http://localhost:9999/ajax?task=init').read())
        self.assertEqual(resp['session_id'], 'abcdef')
        self.assertTrue('error' not in resp)

        self.mox.VerifyAll()

    def test_init_twice(self):
        self.master.make_session('127.0.0.1').AndReturn('abcdef')
        self.master.make_session('127.0.0.1').AndReturn('ghijkl')
        self.mox.ReplayAll()

        resp = yaml.load(urllib2.urlopen('http://localhost:9999/ajax?task=init').read())
        self.assertEqual(resp['session_id'], 'abcdef')
        self.assertTrue('error' not in resp)

        resp = yaml.load(urllib2.urlopen('http://localhost:9999/ajax?task=init').read())
        self.assertTrue('error' not in resp)
        self.assertEqual(resp['session_id'], 'ghijkl')

        self.mox.VerifyAll()

    def test_handle_command_good_session(self):
        session_id = 'abcdef'
        command = 'q file'
        ret = model.CommandResponse('output', False, None, False, False, None, None)
        self.master.handle_command(session_id, command).AndReturn(ret)

        self.mox.ReplayAll()

        resp = yaml.load(urllib2.urlopen('http://localhost:9999/ajax?task=command&session_id=%s&command=%s'
                                         % (session_id, command.replace(' ', '+'))).read())
        self.assertEqual(resp['task'], 'command')
        self.assertEqual(resp['output'], ret.output)
        self.assertEqual(resp['image'], ret.image)
        self.assertEqual(resp['image_data'], ret.image_data)
        self.assertEqual(resp['files_changed'], ret.files_changed)
        self.assertEqual(resp['dims_changed'], ret.dims_changed);
        self.assertEqual(resp['dims'], ret.dims)
        self.assertEqual(resp['ctlinfo'], ret.ctlinfo)
        
        self.assertTrue('done' in resp)
        self.assertTrue('error' not in resp)
        
        self.mox.VerifyAll()

    def test_handle_command_bad_session(self):
        session_id = 'abcdef'
        command = 'q file'
        self.master.handle_command(session_id, command).AndRaise(excepts.SessionError)

        self.mox.ReplayAll()

        resp = yaml.load(urllib2.urlopen('http://localhost:9999/ajax?task=command&session_id=%s&command=%s'
                                         % (session_id, command.replace(' ', '+'))).read())
        self.assertEqual(resp['task'], 'command')
        self.assertTrue(resp['error'])
        self.assertEqual(resp['error'], 1)
        
        self.mox.VerifyAll()
        
    def test_handle_ping_good_session(self):
        session_id = 'abcdef'
        self.master.handle_ping(session_id).AndReturn(None)
        self.mox.ReplayAll()

        resp = yaml.load(urllib2.urlopen('http://localhost:9999/ajax?task=ping&session_id=%s' % session_id).read())
        self.assertEqual(resp['task'], 'ping')
        self.assertTrue('error' not in resp)

        self.mox.VerifyAll()

    def test_handle_ping_bad_session(self):
        session_id = 'abcdef'
        self.master.handle_ping(session_id).AndRaise(excepts.SessionError)
        self.mox.ReplayAll()

        resp = yaml.load(urllib2.urlopen('http://localhost:9999/ajax?task=ping&session_id=%s' % session_id).read())
        self.assertEqual(resp['task'], 'ping')
        self.assertTrue(resp['error'])
        self.assertEqual(resp['error'], 1)

        self.mox.VerifyAll()

        
if __name__ == '__main__':
    unittest.main()
